from django.db import models

from .choices import *


class Department(models.Model):
    # no need to add id
    # id = models.IntegerField(primary_key=True, unique=True, auto_created=True)
    supervisor_id = models.IntegerField(blank=True)
    name = models.CharField(max_length=20, blank=False)

    class Meta:
        ordering = ['id']

    def __str__(self):
        return "(" + str(self.supervisor_id) + ")" + self.name


class Employee(models.Model):
    class Gender(models.IntegerChoices):
        MALE = 1
        FEMALE = 2

    name = models.CharField(max_length=20)
    gender = models.IntegerField(choices=Gender.choices)
    age = models.IntegerField(blank=True)
    department = models.ForeignKey(Department, on_delete=models.CASCADE)
    supervisor = models.IntegerField(blank=True)
    phone = models.CharField(max_length=20, blank=True)
    email = models.CharField(max_length=255, blank=True)

    class Meta:
        ordering = ['id']


class Token(models.Model):
    employee = models.OneToOneField("Employee", on_delete=models.DO_NOTHING)
    token = models.CharField(max_length=100)


class WorkTable(models.Model):
    class StateChoices(models.IntegerChoices):
        temp = 1
        pernament = 2

    start_time = models.TimeField()
    end_time = models.TimeField()
    state = models.IntegerField(choices=StateChoices.choices)

    class Meta:
        ordering = ['id']


class Workshift(models.Model):
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE, parent_link=True)
    worktable = models.ForeignKey(WorkTable, on_delete=models.CASCADE)
    date = models.DateField(blank=True)
    state = models.BooleanField(blank=False)

    class Meta:
        ordering = ['id']


class Attendence(models.Model):
    class StateChoices(models.IntegerChoices):
        ok = 1
        absence = 2
        wait = 3

    time = models.DateTimeField()
    state = models.IntegerField(choices=StateChoices.choices, blank=False)
    workshift = models.ForeignKey(Workshift, on_delete=models.CASCADE)

    class Meta:
        ordering = ['id']


class UserInfo(models.Model):
    name = models.CharField(max_length=20)
    password = models.CharField(max_length=256, blank=False)
    level = models.IntegerField(choices=LevelChoices, blank=False)
    employee = models.OneToOneField(Employee, on_delete=models.CASCADE)

    class Meta:
        ordering = ['id']


class RequestTable(models.Model):
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE)
    start_time = models.DateTimeField(blank=True)
    end_time = models.DateTimeField(blank=True)
    request_type = models.IntegerField(choices=RequestChoice, blank=True)
    result = models.IntegerField(choices=ResultChoice, blank=True)
    leave_T = models.IntegerField(choices=LeaveTypeChoice, blank=True)
    reason = models.TextField(blank=True)
    comment = models.CharField(blank=True, max_length=65535)
    created_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['created_time']


class Leave(models.Model):
    leave_id = models.AutoField(primary_key=True, auto_created=True)
    user_id = models.CharField(max_length=40)
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()
    leave_status = models.CharField(max_length=8)
    leave_reason = models.CharField(max_length=500)
    leave_type = models.CharField(max_length=10)
